package holdem.Rulegui;
import java.io.Serializable;
import java.util.Random;
import java.util.Vector;


public class GeneticCode extends Vector<Gene> implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String ERRORMSG = "Error in Load From GeneCode";
	public static void main(String[] agrs){
		GeneticCode test =new GeneticCode(); 
		test.add(new Gene(100 , true));
		test.add(new Gene(100 , true));
		test.add(new Gene(100 , false));
		test.add(new Gene(100 , true));
		test.mutate(.05);
		String temp = test.saveString();
		System.out.println(temp);
		test = test.Load(temp);
		System.out.println(test.saveString());
		
		
	}
	/**
	 * 
	
	 * @param in
	 * @return geneticCode
	 * note: expects to see GEN:<Gene>|<Gene>;
	 * 
	 */
	public static GeneticCode Load(String  in){
		GeneticCode Rval = new GeneticCode();
		String[] tmp = in.split(":");
		if(!tmp[0].equals("GEN") ){
			System.out.println(ERRORMSG);
			System.exit(0);
			//note could put tryCatch
		}
		//object split by 'B' charictor;
		tmp = tmp[1].split("B");
		for(int i = 0; i < tmp.length; i++){
			//System.out.println(tmp[i]);
			Rval.add(Gene.Load(tmp[i]));
		}
	
		return Rval;
	}
	/**
	 * SaveString
	 * @return return a string that will be == to load
	 */
	public String saveString(){
		String temp ="GEN:";
		
		for(int i = 0; i < this.size(); i++){
			temp += get(i).saveString();
			temp += i == this.size() -1 ? "": "B"; 
		}
		//temp+= "\n"; responiblit to the thing that calls me for my delimiter
		return temp;
	}
	/**
	 * Mutate the genecode note 
	 * @param precent
	 * note this will be more varient than the given prevetage
	 * note a number can become negitive
	 * 
	 */
	public void mutate(Double precent){
		Random rand = new Random();
		for(int i= 0; i < size(); i++){
			if(get(i).isMut()){
				get(i).set( get(i).get()  +((int) (get(i).get() * rand.nextGaussian() * (precent+1))));
			}	
		}
	}
	
	
}
